﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.RequestSecurityTokenForRemoteTokenFactory
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using System.Xml;

namespace Microsoft.InfoCards
{
  internal class RequestSecurityTokenForRemoteTokenFactory : RequestSecurityToken
  {
    public RequestSecurityTokenForRemoteTokenFactory(RequestSecurityTokenParameters rstParams)
      : base(rstParams)
    {
    }

    protected override void WriteAppliesToElement()
    {
      switch (AppliesToBehaviorDecisionTable.GetAppliesToBehaviorDecisionForRst(this.Policy, this.m_rstParams.Card.RequireAppliesto))
      {
        case AppliesToBehaviorDecision.SendRPAppliesTo:
          this.Serializer.WriteAppliesToElement(this.Policy.PolicyAppliesTo, this.m_rstParams.Version);
          break;
        case AppliesToBehaviorDecision.SendCustomAppliesTo:
          this.Serializer.WriteAppliesToElement(this.Policy.ImmediateTokenRecipient.Address, this.m_rstParams.Version);
          break;
      }
    }

    protected override void WriteSecondaryParametersElement()
    {
      if (XmlNames.WSSpecificationVersion.WSTrustOasis2007 != this.ProtocolVersionProfile.WSTrust.Version)
        return;
      bool flag = true;
      if (this.Policy.OptionalClaims.Length > 0)
        flag = this.m_rstParams.DiscloseOptionalClaims && !this.OptionalClaimsExceedCard(this.m_rstParams.Card, this.m_rstParams.Policy);
      switch (AppliesToBehaviorDecisionTable.GetAppliesToBehaviorDecisionForRst(this.Policy, this.m_rstParams.Card.RequireAppliesto))
      {
        case AppliesToBehaviorDecision.SendRPAppliesTo:
        case AppliesToBehaviorDecision.SendCustomAppliesTo:
          if (!flag)
            break;
          if (this.Policy.RelyingPartyPolicy != null)
          {
            this.Serializer.WriteSecondaryParametersElement(this.Policy.RelyingPartyPolicy.PolicyXml);
            break;
          }
          this.Serializer.WriteSecondaryParametersElement(this.Policy.ClientPolicy.PolicyXml);
          break;
        default:
          flag = false;
          goto case AppliesToBehaviorDecision.SendRPAppliesTo;
      }
    }

    protected override void CustomWriteBodyContents(XmlDictionaryWriter writer)
    {
      this.InitializeWriters(writer);
      this.WriteRSTOpeningElement();
      this.WriteRequestTypeElement();
      this.WriteInfoCardReferenceElement();
      this.WriteClaimsElement();
      this.WriteKeyTypeElement();
      this.WriteKeySupportingElements();
      this.WriteAppliesToElement();
      this.WritePPIDElement();
      this.WriteEncryptionAlgorithmElement();
      this.WritePassOnElements();
      this.WriteDisplayTokenElement();
      this.WriteUnprocessedPolicyElements();
      this.WriteSecondaryParametersElement();
      this.WriteEndElement();
    }

    private bool OptionalClaimsExceedCard(InfoCard card, InfoCardPolicy policy)
    {
      InfoCardClaimCollection claims = card.GetClaims();
      bool flag = false;
      foreach (string optionalClaim in policy.OptionalClaims)
      {
        if (!claims.ContainsKey(optionalClaim))
          flag = true;
      }
      return flag;
    }
  }
}
